package org.emet.web;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.emet.help.ExportExcel;
import org.emet.jee.console.entity.sys.AppControl;
import org.emet.service.AppControlService;
import org.emet.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class PrintController {

	@Autowired
	private ReportService reportService;
	@Autowired
	private AppControlService appControlService;

	// It's a common and simple excel solution, commonly, it enough for most of situation.

	@RequestMapping(value = "/print_simple.do", method = { RequestMethod.POST })
	public void print(@RequestParam("title") String title, @RequestParam("headers") String[] headers,
			@RequestParam("dataArray") String[] dataArray, HttpServletResponse response) throws Exception {
		setHeader(title, response);
		ServletOutputStream outputStream = response.getOutputStream();
		ExportExcel.exportExcel(title, headers, dataArray, outputStream);
		outputStream.close();
	}

	//打印接运单
	@RequestMapping(value = "/print_carriage_receipt.do", method = { RequestMethod.POST })
	public void print(@RequestParam("id") Long id, HttpServletResponse response) throws Exception {
		AppControl appControl = appControlService.get(1L);
		String title = appControl.getCompany() + "遗体接运单";
		setHeader(title, response);

		HSSFWorkbook workbook = reportService.genernateCarriageReceipt(title, id);
		try {
			workbook.write(response.getOutputStream());
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	//打印收费清单
	@RequestMapping(value = "/print_charge_receipt.do", method = { RequestMethod.POST })
	public void printChargeReceipt(@RequestParam("id") Long id, HttpServletResponse response) throws Exception {
		AppControl appControl = appControlService.get(1L);
		String title = appControl.getCompany() + "业务消费清单";
		setHeader(title, response);

		HSSFWorkbook workbook = reportService.genernateChargeReceipt(title, id);
		try {
			workbook.write(response.getOutputStream());
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@RequestMapping(value = "/print_month_biz_report.do", method = { RequestMethod.POST })
	public void print(@RequestParam("year") int year, @RequestParam("month") int month, HttpServletResponse response)
			throws Exception {
		String xlsName = reportService.generateStringWithDateTime("yyyy年MM月中心考核");
		setHeader(xlsName, response);
		ServletOutputStream out = response.getOutputStream();

		HSSFWorkbook workbook = reportService.generateMonthlyBizReport(year, month);
		try {
			workbook.write(out);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@RequestMapping(value = "/print_year_biz_report.do", method = { RequestMethod.POST })
	public void print(@RequestParam("year") int year, HttpServletResponse response) throws Exception {
		String xlsName = reportService.generateStringWithDateTime("yyyy年度收入汇总工作表");
		setHeader(xlsName, response);
		ServletOutputStream out = response.getOutputStream();

		HSSFWorkbook workbook = reportService.generateYearlyBizReport(year);
		try {
			workbook.write(out);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	private void setHeader(String fileName, HttpServletResponse response) throws UnsupportedEncodingException {
		String timestamp = reportService.generateStringWithDateTime("yyyyMMddHHmmss");
		String header = MessageFormat.format("attachment;filename={0}_{1}.xls", parseCN(fileName), timestamp);

		response.setCharacterEncoding("GBK");
		response.setContentType("octets/stream");
		response.addHeader("Content-Disposition", header);
	}

	private String parseCN(String cnString) throws UnsupportedEncodingException {
		return new String(cnString.getBytes("GBK"), "ISO8859_1");
	}

}
